//
//  ZCXPetGifViewController.m
//  LoveOfPet
//
//  Created by dllo on 15/11/21.
//  Copyright © 2015年 孔维. All rights reserved.
//

#import "ZCXPetGifViewController.h"
#import "ZFHYNetWork.h"
#import "ZCXGifModel.h"
#import "ZCXPetGifTableViewCell.h"
#import <MJRefresh.h>
#import <SVProgressHUD.h>
#define HEIGHT  [[UIScreen mainScreen]bounds].size.height / 667
#define WIDTH   [[UIScreen mainScreen]bounds].size.width / 375
@interface ZCXPetGifViewController ()<UITableViewDataSource, UITableViewDelegate, ZCXPetGifTableViewCellDelegate>
@property (nonatomic, retain) NSMutableArray *gifArray;
@property (nonatomic, retain) UITableView *tableView;
@property (nonatomic, retain) MJRefreshBackGifFooter *footer;
@property (nonatomic, assign) NSInteger pageNumber;
@property (nonatomic, copy) NSString *mainPathGif;
@property (nonatomic, retain) NSTimer *timer;
@end

@implementation ZCXPetGifViewController
- (void)dealloc{
    [_tableView release];
    [_gifArray release];
    [_footer release];
    [_mainPathGif release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"萌宠GIF";
     self.gifArray = [NSMutableArray array];
    self.view.backgroundColor = [UIColor whiteColor];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];
    self.view.backgroundColor = [UIColor whiteColor];
    [self loadDataWithString:@"http://api.jiefu.tv/app2/api/article/list.html?pageNum=0&versionNo=211&sourceType=1&pageSize=20&deviceCode=860923029360494&typeId=1"];
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height - 64 * HEIGHT) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [self.tableView registerClass:[ZCXPetGifTableViewCell class] forCellReuseIdentifier:@"zcxpetgiftableviewcell"];
    [_tableView release];
    
    self.timer = [NSTimer scheduledTimerWithTimeInterval:43200 target:self selector:@selector(footerRefresh) userInfo:nil repeats:YES];
    
  //  [self Footer];
}
#pragma mark 上拉加载
-(void)Footer{
    self.footer = [MJRefreshBackGifFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefresh)];
    self.tableView.footer = self.footer;
}
- (void)footerRefresh{
    _pageNumber += 1;
    [self loadDataWithString:[NSString stringWithFormat:@"http://api.jiefu.tv/app2/api/article/list.html?pageNum=%ld&versionNo=211&sourceType=1&pageSize=20&deviceCode=860923029360494&typeId=1", _pageNumber]];

}
- (void)loadDataWithString:(NSString *)urlString{
    [ZFHYNetWork handleGETWithUrlString:urlString parameters:nil showHuD:YES onView:nil successfulBlock:^(id responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *array = [dic objectForKey:@"data"];
        if (array.count == 0) {
            _pageNumber = 0;
        }
       
        for (NSDictionary *dicc in array) {
            ZCXGifModel *gif = [ZCXGifModel ZCXGifModelWithdictionary:dicc];
            if ([[NSString stringWithFormat:@"%@", gif.item.mediaType] isEqualToString:@"1"]) {
               
                 [self.gifArray addObject:gif];
            }
        }
        [self.tableView.footer endRefreshing];
        [self.tableView reloadData];
    } failureBlock:^(NSError *error) {
        NSLog(@"失败");
    }];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.gifArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZCXPetGifTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"zcxpetgiftableviewcell" forIndexPath:indexPath];
    cell.gif = self.gifArray[indexPath.section];
    cell.selectionStyle = 0;
    cell.backgroundColor = [UIColor whiteColor];
    
//    [cell sendmainPath:^(NSString *mainpath) {
        if (![self.mainPathGif isEqualToString:[self.gifArray[indexPath.row] mainPicPath]]) {
            cell.imageV.hidden = NO;
            cell.fun = NO;
        }
//    }];
    cell.delegate = self;
    return cell;
}
- (void)sendWithMainPath:(NSString *)mainPath{
    self.mainPathGif = mainPath;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ZCXGifModel *gif = self.gifArray[indexPath.section];
    CGFloat hh = [[[gif item] height] floatValue] / [[[gif item] width] floatValue];
    return hh * (self.view.frame.size.width - 10) + 80;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}
- (void)viewDidDisappear:(BOOL)animated{
    [self.timer invalidate];
    [_timer release];
}
- (void)viewWillDisappear:(BOOL)animated{
    [SVProgressHUD dismiss];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
